package o;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Pair;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.IllegalSeekPositionException;
import com.google.android.exoplayer2.Player.d;
import com.google.android.exoplayer2.Renderer;
import com.google.android.exoplayer2.source.TrackGroupArray;
import java.util.ArrayDeque;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import o.abr.c;
import o.aim.a;

final class abe extends aaz implements abc {
    private final abf ʻ;
    private int ʻॱ;
    private final c ʼ;
    private long ʼॱ;
    private final CopyOnWriteArraySet<d> ʽ;
    private abp ʽॱ;
    private int ʾ;
    private int ʿ;
    @Nullable
    private ExoPlaybackException ˈ;
    private final alb ˊ;
    private boolean ˊॱ;
    private final Handler ˋ;
    private boolean ˋॱ;
    private final Renderer[] ˎ;
    private boolean ˏॱ;
    private aim ͺ;
    final ala ॱ;
    private int ॱˊ;
    private boolean ॱˋ;
    private abq ॱˎ;
    private final Handler ॱॱ;
    private abv ॱᐝ;
    private final ArrayDeque<e> ᐝ;
    private boolean ᐝॱ;

    static final class e {
        private final boolean ʻ;
        private final boolean ʼ;
        private final boolean ʽ;
        private final boolean ˊ;
        private final Set<d> ˋ;
        private final boolean ˋॱ;
        private final abp ˎ;
        private final alb ˏ;
        private final boolean ͺ;
        private final int ॱ;
        private final boolean ॱॱ;
        private final int ᐝ;

        public e(abp o_abp, abp o_abp2, Set<d> set, alb o_alb, boolean z, int i, int i2, boolean z2, boolean z3, boolean z4) {
            boolean z5 = true;
            this.ˎ = o_abp;
            this.ˋ = set;
            this.ˏ = o_alb;
            this.ˊ = z;
            this.ॱ = i;
            this.ᐝ = i2;
            this.ʽ = z2;
            this.ʼ = z3;
            boolean z6 = z4 || o_abp2.ʻ != o_abp.ʻ;
            this.ॱॱ = z6;
            if (o_abp2.ˊ == o_abp.ˊ && o_abp2.ˎ == o_abp.ˎ) {
                z6 = false;
            } else {
                z6 = true;
            }
            this.ʻ = z6;
            if (o_abp2.ʼ != o_abp.ʼ) {
                z6 = true;
            } else {
                z6 = false;
            }
            this.ˋॱ = z6;
            if (o_abp2.ॱॱ == o_abp.ॱॱ) {
                z5 = false;
            }
            this.ͺ = z5;
        }

        public void ˋ() {
            if (this.ʻ || this.ᐝ == 0) {
                for (d ॱ : this.ˋ) {
                    ॱ.ॱ(this.ˎ.ˊ, this.ˎ.ˎ, this.ᐝ);
                }
            }
            if (this.ˊ) {
                for (d ॱ2 : this.ˋ) {
                    ॱ2.ˎ(this.ॱ);
                }
            }
            if (this.ͺ) {
                this.ˏ.ˏ(this.ˎ.ॱॱ.ˊ);
                for (d ॱ22 : this.ˋ) {
                    ॱ22.ˊ(this.ˎ.ʽ, this.ˎ.ॱॱ.ˏ);
                }
            }
            if (this.ˋॱ) {
                for (d ॱ222 : this.ˋ) {
                    ॱ222.ˎ(this.ˎ.ʼ);
                }
            }
            if (this.ॱॱ) {
                for (d ॱ2222 : this.ˋ) {
                    ॱ2222.ˏ(this.ʼ, this.ˎ.ʻ);
                }
            }
            if (this.ʽ) {
                for (d ॱ22222 : this.ˋ) {
                    ॱ22222.ˊ();
                }
            }
        }
    }

    @SuppressLint({"HandlerLeak"})
    public abe(Renderer[] rendererArr, alb o_alb, abi o_abi, ald o_ald, ams o_ams, Looper looper) {
        amz.ॱ("ExoPlayerImpl", "Init " + Integer.toHexString(System.identityHashCode(this)) + " [" + "ExoPlayerLib/2.9.6" + "] [" + anr.ॱ + "]");
        amm.ˏ(rendererArr.length > 0);
        this.ˎ = (Renderer[]) amm.ॱ(rendererArr);
        this.ˊ = (alb) amm.ॱ(o_alb);
        this.ˋॱ = false;
        this.ॱˊ = 0;
        this.ˊॱ = false;
        this.ʽ = new CopyOnWriteArraySet();
        this.ॱ = new ala(new abs[rendererArr.length], new akz[rendererArr.length], null);
        this.ʼ = new c();
        this.ॱˎ = abq.ˎ;
        this.ॱᐝ = abv.ॱ;
        this.ˋ = new Handler(this, looper) {
            final /* synthetic */ abe ˎ;

            public void handleMessage(Message message) {
                this.ˎ.ˊ(message);
            }
        };
        this.ʽॱ = abp.ˊ(0, this.ॱ);
        this.ᐝ = new ArrayDeque();
        this.ʻ = new abf(rendererArr, o_alb, this.ॱ, o_abi, o_ald, this.ˋॱ, this.ॱˊ, this.ˊॱ, this.ˋ, o_ams);
        this.ॱॱ = new Handler(this.ʻ.ˊ());
    }

    public Looper ˎ() {
        return this.ˋ.getLooper();
    }

    public void ॱ(d dVar) {
        this.ʽ.add(dVar);
    }

    public void ˏ(d dVar) {
        this.ʽ.remove(dVar);
    }

    public int ˏ() {
        return this.ʽॱ.ʻ;
    }

    public void ॱ(aim o_aim, boolean z, boolean z2) {
        this.ˈ = null;
        this.ͺ = o_aim;
        abp ˊ = ˊ(z, z2, 2);
        this.ॱˋ = true;
        this.ʻॱ++;
        this.ʻ.ˋ(o_aim, z, z2);
        ˊ(ˊ, false, 4, 1, false, false);
    }

    public void ˊ(boolean z, boolean z2) {
        boolean z3 = z && !z2;
        if (this.ˏॱ != z3) {
            this.ˏॱ = z3;
            this.ʻ.ˏ(z3);
        }
        if (this.ˋॱ != z) {
            this.ˋॱ = z;
            ˊ(this.ʽॱ, false, 4, 1, false, true);
        }
    }

    public boolean ˊ() {
        return this.ˋॱ;
    }

    public void ॱ(int i) {
        if (this.ॱˊ != i) {
            this.ॱˊ = i;
            this.ʻ.ˎ(i);
            Iterator it = this.ʽ.iterator();
            while (it.hasNext()) {
                ((d) it.next()).ˋ(i);
            }
        }
    }

    public void ˏ(int i, long j) {
        abr o_abr = this.ʽॱ.ˊ;
        if (i < 0 || (!o_abr.ˏ() && i >= o_abr.ॱ())) {
            throw new IllegalSeekPositionException(o_abr, i, j);
        }
        this.ᐝॱ = true;
        this.ʻॱ++;
        if (ˏॱ()) {
            amz.ˏ("ExoPlayerImpl", "seekTo ignored because an ad is playing");
            this.ˋ.obtainMessage(0, 1, -1, this.ʽॱ).sendToTarget();
            return;
        }
        this.ʾ = i;
        if (o_abr.ˏ()) {
            this.ʼॱ = j == -9223372036854775807L ? 0 : j;
            this.ʿ = 0;
        } else {
            long ˊ = j == -9223372036854775807L ? o_abr.ˎ(i, this.ˏ).ˊ() : C.ˏ(j);
            Pair ˏ = o_abr.ˏ(this.ˏ, this.ʼ, i, ˊ);
            this.ʼॱ = C.ˋ(ˊ);
            this.ʿ = o_abr.ˎ(ˏ.first);
        }
        this.ʻ.ˊ(o_abr, i, C.ˏ(j));
        Iterator it = this.ʽ.iterator();
        while (it.hasNext()) {
            ((d) it.next()).ˎ(1);
        }
    }

    public void ˊ(@Nullable abv o_abv) {
        if (o_abv == null) {
            o_abv = abv.ॱ;
        }
        if (!this.ॱᐝ.equals(o_abv)) {
            this.ॱᐝ = o_abv;
            this.ʻ.ˎ(o_abv);
        }
    }

    public void ˊ(boolean z) {
        if (z) {
            this.ˈ = null;
            this.ͺ = null;
        }
        abp ˊ = ˊ(z, z, 1);
        this.ʻॱ++;
        this.ʻ.ˋ(z);
        ˊ(ˊ, false, 4, 1, false, false);
    }

    public void ʻ() {
        amz.ॱ("ExoPlayerImpl", "Release " + Integer.toHexString(System.identityHashCode(this)) + " [" + "ExoPlayerLib/2.9.6" + "] [" + anr.ॱ + "] [" + abh.ˏ() + "]");
        this.ͺ = null;
        this.ʻ.ˏ();
        this.ˋ.removeCallbacksAndMessages(null);
    }

    public abn ॱ(abn.c cVar) {
        return new abn(this.ʻ, cVar, this.ʽॱ.ˊ, ॱॱ(), this.ॱॱ);
    }

    public int ʽ() {
        if (ʻॱ()) {
            return this.ʿ;
        }
        return this.ʽॱ.ˊ.ˎ(this.ʽॱ.ॱ.ˋ);
    }

    public int ॱॱ() {
        if (ʻॱ()) {
            return this.ʾ;
        }
        return this.ʽॱ.ˊ.ॱ(this.ʽॱ.ॱ.ˋ, this.ʼ).ॱ;
    }

    public long ᐝ() {
        if (!ˏॱ()) {
            return ॱ();
        }
        a aVar = this.ʽॱ.ॱ;
        this.ʽॱ.ˊ.ॱ(aVar.ˋ, this.ʼ);
        return C.ˋ(this.ʼ.ˊ(aVar.ॱ, aVar.ˊ));
    }

    public long ʼ() {
        if (ʻॱ()) {
            return this.ʼॱ;
        }
        if (this.ʽॱ.ॱ.ˊ()) {
            return C.ˋ(this.ʽॱ.ͺ);
        }
        return ॱ(this.ʽॱ.ॱ, this.ʽॱ.ͺ);
    }

    public long ॱˊ() {
        if (!ˏॱ()) {
            return ॱᐝ();
        }
        if (this.ʽॱ.ᐝ.equals(this.ʽॱ.ॱ)) {
            return C.ˋ(this.ʽॱ.ॱˊ);
        }
        return ᐝ();
    }

    public long ˊॱ() {
        return Math.max(0, C.ˋ(this.ʽॱ.ˋॱ));
    }

    public boolean ˏॱ() {
        return !ʻॱ() && this.ʽॱ.ॱ.ˊ();
    }

    public int ͺ() {
        return ˏॱ() ? this.ʽॱ.ॱ.ॱ : -1;
    }

    public int ˋॱ() {
        return ˏॱ() ? this.ʽॱ.ॱ.ˊ : -1;
    }

    public long ॱˎ() {
        if (!ˏॱ()) {
            return ʼ();
        }
        this.ʽॱ.ˊ.ॱ(this.ʽॱ.ॱ.ˋ, this.ʼ);
        return this.ʼ.ˋ() + C.ˋ(this.ʽॱ.ˏ);
    }

    public long ॱᐝ() {
        if (ʻॱ()) {
            return this.ʼॱ;
        }
        if (this.ʽॱ.ᐝ.ˎ != this.ʽॱ.ॱ.ˎ) {
            return this.ʽॱ.ˊ.ˎ(ॱॱ(), this.ˏ).ˏ();
        }
        long j = this.ʽॱ.ॱˊ;
        if (this.ʽॱ.ᐝ.ˊ()) {
            c ॱ = this.ʽॱ.ˊ.ॱ(this.ʽॱ.ᐝ.ˋ, this.ʼ);
            j = ॱ.ˋ(this.ʽॱ.ᐝ.ॱ);
            if (j == Long.MIN_VALUE) {
                j = ॱ.ˎ;
            }
        }
        return ॱ(this.ʽॱ.ᐝ, j);
    }

    public abr ᐝॱ() {
        return this.ʽॱ.ˊ;
    }

    void ˊ(Message message) {
        Iterator it;
        switch (message.what) {
            case 0:
                boolean z;
                abp o_abp = (abp) message.obj;
                int i = message.arg1;
                if (message.arg2 != -1) {
                    z = true;
                } else {
                    z = false;
                }
                ˊ(o_abp, i, z, message.arg2);
                return;
            case 1:
                abq o_abq = (abq) message.obj;
                if (!this.ॱˎ.equals(o_abq)) {
                    this.ॱˎ = o_abq;
                    it = this.ʽ.iterator();
                    while (it.hasNext()) {
                        ((d) it.next()).ˎ(o_abq);
                    }
                    return;
                }
                return;
            case 2:
                ExoPlaybackException exoPlaybackException = (ExoPlaybackException) message.obj;
                this.ˈ = exoPlaybackException;
                it = this.ʽ.iterator();
                while (it.hasNext()) {
                    ((d) it.next()).ˊ(exoPlaybackException);
                }
                return;
            default:
                throw new IllegalStateException();
        }
    }

    private void ˊ(abp o_abp, int i, boolean z, int i2) {
        this.ʻॱ -= i;
        if (this.ʻॱ == 0) {
            abp ˎ;
            if (o_abp.ˋ == -9223372036854775807L) {
                ˎ = o_abp.ˎ(o_abp.ॱ, 0, o_abp.ˏ);
            } else {
                ˎ = o_abp;
            }
            if ((!this.ʽॱ.ˊ.ˏ() || this.ॱˋ) && ˎ.ˊ.ˏ()) {
                this.ʿ = 0;
                this.ʾ = 0;
                this.ʼॱ = 0;
            }
            int i3 = this.ॱˋ ? 0 : 2;
            boolean z2 = this.ᐝॱ;
            this.ॱˋ = false;
            this.ᐝॱ = false;
            ˊ(ˎ, z, i2, i3, z2, false);
        }
    }

    private abp ˊ(boolean z, boolean z2, int i) {
        abr o_abr;
        Object obj;
        TrackGroupArray trackGroupArray;
        ala o_ala;
        if (z) {
            this.ʾ = 0;
            this.ʿ = 0;
            this.ʼॱ = 0;
        } else {
            this.ʾ = ॱॱ();
            this.ʿ = ʽ();
            this.ʼॱ = ʼ();
        }
        a ॱ = z ? this.ʽॱ.ॱ(this.ˊॱ, this.ˏ) : this.ʽॱ.ॱ;
        long j = z ? 0 : this.ʽॱ.ͺ;
        long j2 = z ? -9223372036854775807L : this.ʽॱ.ˏ;
        if (z2) {
            o_abr = abr.ˊ;
        } else {
            o_abr = this.ʽॱ.ˊ;
        }
        if (z2) {
            obj = null;
        } else {
            obj = this.ʽॱ.ˎ;
        }
        if (z2) {
            trackGroupArray = TrackGroupArray.ˎ;
        } else {
            trackGroupArray = this.ʽॱ.ʽ;
        }
        if (z2) {
            o_ala = this.ॱ;
        } else {
            o_ala = this.ʽॱ.ॱॱ;
        }
        return new abp(o_abr, obj, ॱ, j, j2, i, false, trackGroupArray, o_ala, ॱ, j, 0, j);
    }

    private void ˊ(abp o_abp, boolean z, int i, int i2, boolean z2, boolean z3) {
        Object obj;
        if (this.ᐝ.isEmpty()) {
            obj = null;
        } else {
            obj = 1;
        }
        this.ᐝ.addLast(new e(o_abp, this.ʽॱ, this.ʽ, this.ˊ, z, i, i2, z2, this.ˋॱ, z3));
        this.ʽॱ = o_abp;
        if (obj == null) {
            while (!this.ᐝ.isEmpty()) {
                ((e) this.ᐝ.peekFirst()).ˋ();
                this.ᐝ.removeFirst();
            }
        }
    }

    private long ॱ(a aVar, long j) {
        long ˋ = C.ˋ(j);
        this.ʽॱ.ˊ.ॱ(aVar.ˋ, this.ʼ);
        return ˋ + this.ʼ.ˋ();
    }

    private boolean ʻॱ() {
        return this.ʽॱ.ˊ.ˏ() || this.ʻॱ > 0;
    }
}
